package handler

import (
	"gin-products/api/request"
	"gin-products/inter/dao"
	"gin-products/inter/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddComment(c *gin.Context) {
	var req request.AddCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var comment model.Comment
	var pro model.Product
	dao.GetOneByFields(&model.Product{
		Id: req.ProductId,
	}, &pro)
	if pro.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品不存在，无法评价",
			"data": nil,
		})
		return
	}
	comment = model.Comment{

		UserId:    req.UserId,
		ProductId: req.ProductId,
		Commtent:  req.Commtent,
	}
	if !dao.Create(&comment) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "评论发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "评论发布成功",
		"data": comment,
	})
}

func UpdateComment(c *gin.Context) {
	var req request.UpdateCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var comment model.Comment

	comment = model.Comment{
		Id: req.Id,

		Commtent: req.Commtent,
	}
	if !dao.Update(&comment) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "评论修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "评论修改成功",
		"data": comment,
	})
}

func DelComment(c *gin.Context) {
	var req request.DelCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var comment model.Comment
	dao.GetOneByFields(&model.Comment{
		Id: req.Id,
	}, &comment)
	if comment.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "评论不存在",
			"data": nil,
		})
		return
	}
	comment = model.Comment{
		Id: req.Id,
	}
	if !dao.Delete(&comment) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "评论删除失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "评论删除成功",
		"data": true,
	})
}

func GetComment(c *gin.Context) {

}
